import { createMemoryHistory, createRouter,createWebHistory } from 'vue-router'

import login from '@/page/LoginPage/LoginPage.vue'
import index from '@/page/indexPage/index.vue'
import ops from '@/page/opsPage/ops.vue'
import cluster from '@/page/clusterPage/cluster.vue'
import setting from '@/page/settingPage/setting.vue'
import opsMenu from '@/page/opsPage/components/opsMenu.vue'
import opsDoProject from '@/page/opsPage/components/opsDophinProject.vue'
import opsDophinMenu from '@/page/opsPage/components/opsDophinMenu.vue'
import opsProcessDefine from '@/page/opsPage/components/opsProcessDefine.vue'
import opsProcessInstance from '@/page/opsPage/components/opsProcessInstance.vue'
import opsTaskInstance from '@/page/opsPage/components/opsTaskInstance.vue'
import test from '@/page/opsPage/components/test.vue'
import { useAuthStore } from '@/stores/auth'

const routes = [
  { path: '/login',
    name: 'Login',
    component: login,
    meta: { notNeedAuth: true }
  },
  { path: '/', 
    name: 'index',
    component: index 
  },
  { path: '/ops', 
    component: ops,
    children: [
      {
        path: '',
        component: opsMenu,
      },
      {
        path: 'dolphin/:clusterId/project',
        name: 'DolphinProject',
        component: opsDoProject,
      },
      {
        path: 'dolphin/:clusterId/project/:projectCode/dophinmenu',
        name: 'DolphinMenu',
        component: opsDophinMenu,
      },
      {
        path: 'dolphin/:clusterId/project/:projectCode/opsProcessDefine',
        name: 'opsProcessDefine',
        component: opsProcessDefine,
      },
      {
        path: 'dolphin/:clusterId/project/:projectCode/opsProcessInstance',
        name: 'opsProcessInstance',
        component: opsProcessInstance,
      },
      {
        path: 'dolphin/:clusterId/project/:projectCode/task-instance',
        name: 'opsTaskInstance',
        component: () => import('@/page/opsPage/components/opsTaskInstance.vue'),
      },
      {
        path: 'dolphin/:clusterId/project/:projectCode/workflow-tasks/:processInstanceId/:processInstanceName',
        name: 'opsWorkflowTasks',
        component: () => import('@/page/opsPage/components/opsWorkflowTasks.vue'),
        props: true
      },
      {
        path: 'flink/:clusterId',
        name: 'FlinkMenu',
        component: () => import('@/page/opsPage/components/opsFlinkMenu.vue'),
      },
      {
        path: 'flink/:clusterId/jobs',
        name: 'opsFlinkJobs',
        component: () => import('@/page/opsPage/components/opsFlinkJobs.vue'),
      },
      {
        path: 'flink/:clusterId/jars',
        name: 'opsFlinkJars',
        component: () => import('@/page/opsPage/components/opsFlinkJars.vue'),
      },
      {
        path: 'flink/:clusterId/configs',
        name: 'opsFlinkConfigs',
        component: () => import('@/page/opsPage/components/opsFlinkConfigs.vue'),
      }
    ],
  },
  { path: '/cluster', 
    component: cluster },
  { path: '/setting', 
    component: setting },
  {
    path: '/test',
    name: 'test',
    component: test,
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();
  const notNeedAuth = to.matched.some(record => record.meta.notNeedAuth);

  const token = authStore.token || localStorage.getItem('token');

  if (!token && !notNeedAuth) {
    // 如果没有 token 并且目标路由需要认证，则重定向到登录页面
    next({ name: 'Login' });
  } else {
    // 如果有 token 但 store 中没有，则更新 store
    if (token && !authStore.token) {
      authStore.setToken(token);
    }
    next();
  }
});


export default router;